package com.chen.gcode.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


import java.io.*;
import java.net.URI;

public class Setting {
    private static Setting singleton = null;

    private String power;

    private String speed;

    private String savePath;

    private String wdFilePath;

    private Float pointSize;

    private String lastFileDir;

    private String deepDistance;

    private String safeDistance;


    public Setting() {

    }

    public static Setting getInstance() {
        if (singleton == null) {
            Setting setting = readFromFile();

            if (setting != null){
                singleton = setting;
            }else {
                singleton = new Setting();
            }

        }
        return singleton;
    }


    public void save(){
        File f2 = new File(JarTool.getJarDir()+File.separator+"setting.json");
        JSONObject json = (JSONObject)JSON.toJSON(this);
        try {
            // 保证创建一个新文件
            File file = new File(f2.toURI());
            if (!file.getParentFile().exists()) { // 如果父目录不存在，创建父目录
                file.getParentFile().mkdirs();
            }
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            file.createNewFile();
            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(json.toString());
            write.flush();
            write.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Setting readFromFile(){

        File f2 = new File(JarTool.getJarDir()+File.separator+"setting.json");
        String path2 = AppContext.baseDirectory();
        System.out.println(path2);
        if (f2 == null || !f2.exists()){
            Setting setting = new Setting();
            setting.power = "100.00";
            setting.speed = "1000";
            setting.savePath = JarTool.getJarDir()+File.separator;
            JSONObject json = (JSONObject)JSON.toJSON(setting);
//            JSONObject json = JSONObject.fromObject(setting);
            try {
                // 保证创建一个新文件
                File file = new File(f2.toURI());
                if (!file.getParentFile().exists()) { // 如果父目录不存在，创建父目录
                    file.getParentFile().mkdirs();
                }
                if (file.exists()) { // 如果已存在,删除旧文件
                    file.delete();
                }
                file.createNewFile();
                // 将格式化后的字符串写入文件
                Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
                write.write(json.toString());
                write.flush();
                write.close();
            } catch (Exception e) {
                e.printStackTrace();
            }



            return setting;
        }else {
           String json = readJsonFile(f2.toURI());
           System.out.println(json);
//           JSONObject jsonObject = JSONObject.fromObject(json);
            JSONObject jsonObject = (JSONObject)JSON.parse(json);
            Setting setting = new Setting();

            setting  = (Setting)jsonObject.toJavaObject(Setting.class);
           return setting;
        }
    }
    //读取json文件
    public static String readJsonFile(URI path) {
        String jsonStr = "";
        try {
            File jsonFile = new File(path);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Setting getSingleton() {
        return singleton;
    }

    public static void setSingleton(Setting singleton) {
        Setting.singleton = singleton;
    }

    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public String getWdFilePath() {
        return wdFilePath;
    }

    public void setWdFilePath(String wdFilePath) {
        this.wdFilePath = wdFilePath;
    }


    public Float getPointSize() {
        if (pointSize == null){
            pointSize = 1.0f;
        }
        return pointSize;
    }

    public void setPointSize(Float pointSize) {
        this.pointSize = pointSize;
    }

    public String getLastFileDir() {
        return lastFileDir;
    }

    public void setLastFileDir(String lastFileDir) {
        this.lastFileDir = lastFileDir;
    }

    public String getDeepDistance() {
        if (deepDistance == null){
            deepDistance = "1.00";
        }
        return deepDistance;
    }

    public void setDeepDistance(String deepDistance) {
        this.deepDistance = deepDistance;
    }

    public String getSafeDistance() {
        if (safeDistance == null){
            safeDistance = "10";
        }
        return safeDistance;
    }

    public void setSafeDistance(String safeDistance) {
        this.safeDistance = safeDistance;
    }
}
